<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/**
 * Create a new post
 * 
 * @package Xaraya eXtensible Management System
 * @copyright (C) 2005 by the Xaraya Development Team.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.org
 *
 * @subpackage  xarbb Module
 * @author John Cox
*/
/**
 * create a new tag
 * @param $args['tagid'] integer
 * @param $args['uid'] integer
 * @param $args['text'] string
 * @returns int
 * @return tagid on success, false on failure
 */
function conversations_userapi_createposts($args)
{
    extract($args);

    // Argument check - make sure that all required arguments are present,
    // if not then set an appropriate error message and return
    $invalid = array();
    if (!isset($text) || !is_string($text)) {
        $invalid[] = 'post';
    } 
    if (!isset($uid) || !is_numeric($uid)) {
        $invalid[] = 'uid';
    }
    if (count($invalid) > 0) {
        $msg = xarML('Invalid #(1)', join(', ', $invalid));
        xarErrorSet(XAR_SYSTEM_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    } 

    //Let's set defaults so we can pass in values if necessary
    if ((!isset($status)) || (empty($status))){
        $status=0;
    }
    if ((!isset($count)) || (empty($count))){
        $count=0;
    }
    if ((!isset($options))|| (empty($options))){
        $options = serialize(null);
    }
    if ((!isset($timestamp))|| (empty($timestamp))){
        $timestamp = time();
    }
    
    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['cposts'];
    // Get next ID in table
    $nextId = $dbconn->GenId($table);

    // Add item
    $query = "INSERT INTO $table ("
        . " xar_postid, xar_timestamp, xar_uid,"
        . " xar_text, xar_status, xar_count, xar_options)"
        . " VALUES (?, ?, ?, ?, ?, ?, ?)";
    $result =& $dbconn->Execute($query, array($nextId, $timestamp, $uid, $text, $status, $count, $options));
    if (!$result) return;
    // Get the ID of the item that we inserted
    $id = $dbconn->PO_Insert_ID($table, 'xar_postid');

    // Let any hooks know that we have created a new forum
    $args['module'] = 'conversations';
    $args['itemtype'] = 1; // post
    $args['itemid'] = $id; // post
    xarModCallHooks('item', 'create', $id, $args);// post
    // Return the id of the newly created link to the calling process
    return $id;
}
?>